﻿using CleverDD.Helper.Communication.PLC;
using CleverDD.Helper.Tool;
using CleverDD.Model.Communication.PLC;
using System.Collections.Generic;
using System.Linq;

// ReSharper disable once CheckNamespace
namespace CleverDD.Helper
{
    public partial class StaticTool
    {

        public static List<PlcInfoEntity> PlcInfoList = new List<PlcInfoEntity>();
        public static List<PlcAddressInfoEntity> PlcAddressInfoList = new List<PlcAddressInfoEntity>();
       
        public static List<PlcTool> PlcToolList = new List<PlcTool>();


        public static void InitStaticAllPlcInfoList()
        {
            PlcInfoList = JsonParam.LoadParamPath<List<PlcInfoEntity>>(PlcSavePath, PlcInfoListSaveName) ??
                          new List<PlcInfoEntity>();

            PlcAddressInfoList = JsonParam.LoadParamPath<List<PlcAddressInfoEntity>>(PlcSavePath, PlcAddressInfoListSaveName) ??
                                 new List<PlcAddressInfoEntity>();

        }

        public static List<PlcInfoEntity> LoadPlcInfoList()
        {
            List<PlcInfoEntity> plcInfoEntityList =
                JsonParam.LoadParamPath<List<PlcInfoEntity>>(PlcSavePath, PlcInfoListSaveName) ?? new List<PlcInfoEntity>();
            return plcInfoEntityList;
        }

        public static bool SavaPlcInfoList(List<PlcInfoEntity> plcInfoEntityList)
        {
            plcInfoEntityList = plcInfoEntityList.OrderBy(s => s.SortIndex).ToList();
            return JsonParam.SaveParamPath(PlcSavePath, PlcInfoListSaveName, plcInfoEntityList);
        }

        public static List<PlcAddressInfoEntity> LoadPlcAddressInfoList()
        {
            List<PlcAddressInfoEntity> plcAddressInfoEntityList =
                JsonParam.LoadParamPath<List<PlcAddressInfoEntity>>(PlcSavePath, PlcAddressInfoListSaveName) ?? new List<PlcAddressInfoEntity>();
            return plcAddressInfoEntityList;
        }

        public static bool SavaPlcAddressInfoList(List<PlcAddressInfoEntity> plcAddressInfoEntityList)
        {
            plcAddressInfoEntityList = plcAddressInfoEntityList.OrderBy(s => s.SortIndex).ToList();
            return JsonParam.SaveParamPath(PlcSavePath, PlcAddressInfoListSaveName, plcAddressInfoEntityList);
        }


        #region Tool相关
        

        /// <summary>
        /// 根据PlcInfo信息，初始化对应的工具集合，不打开关闭Plc
        /// </summary>
        public static void InitPlcToolList()
        {
            PlcToolList.Clear();
            foreach (var item in PlcInfoList)
            {
                PlcToolList.Add(new PlcTool(item));
            }
        }

        public static void OpenAllPlc()
        {
            foreach (var item in PlcToolList)
            {
                item.ConnPlc();
            }
        }

        public static void CloseAllPlc()
        {
            foreach (var item in PlcToolList)
            {
                item.ClosePlc();
            }
        }


        public static void ReadAllPlc()
        {
            foreach (var item in PlcToolList)
            {
                item.ReadAll();
            }
        }

        #endregion

    }
}
